package com.ruoyi.api.controller;

import java.util.List;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.vim.domain.ImWelcomeWord;
import com.ruoyi.vim.service.IImWelcomeWordService;

/**
 * 欢迎语配置Controller
 *
 * @author wgp
 * @since 2022-12-10
 */
@RestController
@RequestMapping("/api/im/welcomeWords")
public class ImWelcomeWordController extends BaseController<ImWelcomeWord> {
    @Autowired
    private IImWelcomeWordService imWelcomeWordService;

    /**
     * 新增欢迎语配置
     */
    @PostMapping
    public AjaxResult add(@RequestBody ImWelcomeWord imWelcomeWord) {
        return toAjax(imWelcomeWordService.add(imWelcomeWord));
    }

    /**
     * 修改欢迎语配置
     */
    @PutMapping
    public AjaxResult edit(@RequestBody ImWelcomeWord imWelcomeWord) {
        return toAjax(imWelcomeWordService.edit(imWelcomeWord));
    }

    /**
     * 列表
     *
     * @param imWelcomeWord 实体
     */
    @GetMapping
    public AjaxResult list(ImWelcomeWord imWelcomeWord) {
        QueryWrapper<ImWelcomeWord> queryWrapper = getQueryWrapper(imWelcomeWord);
        List<ImWelcomeWord> list = imWelcomeWordService.list(queryWrapper);
        return AjaxResult.success(list);
    }

    /**
     * 获取欢迎语配置详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(imWelcomeWordService.getById(id));
    }

    /**
     * 删除欢迎语配置
     */
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return toAjax(imWelcomeWordService.removeById(id));
    }

    /**
     * 设置默认欢迎语
     */
    @PutMapping("/setDefault")
    public AjaxResult setDefault(@RequestBody ImWelcomeWord imWelcomeWord) {
        return toAjax(imWelcomeWordService.setDefault(imWelcomeWord));
    }

    private QueryWrapper<ImWelcomeWord> getQueryWrapper(ImWelcomeWord imWelcomeWord) {
        QueryWrapper<ImWelcomeWord> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(imWelcomeWord.getWelcomeWord())) {
            queryWrapper.eq("welcome_word", imWelcomeWord.getWelcomeWord());
        }
        if (StringUtils.isNotEmpty(imWelcomeWord.getStatus())) {
            queryWrapper.eq("status", imWelcomeWord.getStatus());
        }
        if (StringUtils.isNotEmpty(imWelcomeWord.getIsDefault())) {
            queryWrapper.eq("is_default", imWelcomeWord.getIsDefault());
        }
        return queryWrapper;
    }
}
